package com.tang.player.ui.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.tang.player.R;
import com.tang.player.listeners.IMediaPlayer;
import com.tang.player.tools.DelayedTask;

/**
 * @author txf
 * @Title 播放控制类
 * @package com.tang.player.ui
 * @date 2017/3/24 0024
 */

public class PlayerControlWidget2 extends GestureControl implements View.OnClickListener, DelayedTask.OnDelayedTaskListener, SeekBar.OnSeekBarChangeListener {
    FrameLayout controlView;
    TextView playView, backView, currentTimeView, durationView, fullscreenView;
    private SeekBar seekBar;
    private boolean isHideControlBar;

    private DelayedTask mDelayedTask;//控制层隐藏显示延时任务
    private DelayedTask mDelayedTask2;//刷新播放进度延时任务

    public PlayerControlWidget2(Context context) {
        super(context);
        init();
    }

    public PlayerControlWidget2(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlayerControlWidget2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_player_control, this);
        controlView = (FrameLayout) findViewById(R.id.view_player_control);
        playView = (TextView) findViewById(R.id.view_player_control_mPlay);
        backView = (TextView) findViewById(R.id.view_player_control_back);
        currentTimeView = (TextView) findViewById(R.id.view_player_control_mCurrentTime);
        durationView = (TextView) findViewById(R.id.view_player_control_mDuration);

        fullscreenView = (TextView) findViewById(R.id.view_player_control_mFullscreen);
        seekBar = (SeekBar) findViewById(R.id.view_player_control_mSeekBar);

        playView.setOnClickListener(this);
        backView.setOnClickListener(this);
        fullscreenView.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(this);

        mDelayedTask = new DelayedTask(this);
        mDelayedTask2 = new DelayedTask(this);
    }

    @Override
    public void startPlayer(IMediaPlayer iMediaPlaye) {
        super.startPlayer(iMediaPlaye);
        currentTimeView.setText(timeFormat(mCurrentTime));
        durationView.setText(timeFormat(mMaxTime));
        playView.setSelected(mPauseOrStart);
        seekBar.setMax((int) (mMaxTime / 1000));
        seekBar.setProgress((int) (mCurrentTime / 1000));
        mDelayedTask.startForwardTimer(4000, 0);
        mDelayedTask2.startForwardTimer(1000, 1);
    }

    @Override
    public void errorPlayer() {
        super.errorPlayer();
        currentTimeView.setText(timeFormat(mCurrentTime));
        durationView.setText(timeFormat(mMaxTime));
        seekBar.setMax((int) (mMaxTime / 1000));
        seekBar.setProgress((int) (mCurrentTime / 1000));
        playView.setSelected(mPauseOrStart);
        showControlBar();
        mDelayedTask.stopForwardTimer();
        mDelayedTask2.stopForwardTimer();
    }

    @Override
    public void completionPlayer() {
        super.completionPlayer();
        currentTimeView.setText(timeFormat(mCurrentTime));
        seekBar.setProgress((int) (mCurrentTime / 1000));
        playView.setSelected(mPauseOrStart);
        showControlBar();
        mDelayedTask.stopForwardTimer();
        mDelayedTask2.stopForwardTimer();
    }

    @Override
    protected boolean doubleTap(MotionEvent e) {
        if (iMediaPlaye != null) {
            if (iMediaPlaye.getCurrentState() == iMediaPlaye.STATE_STARTED) {
                iMediaPlaye.pause();
                mPauseOrStart = false;
                playView.setSelected(mPauseOrStart);
            } else if (iMediaPlaye.getCurrentState() == iMediaPlaye.STATE_PAUSED) {
                iMediaPlaye.start();
                mPauseOrStart = true;
                playView.setSelected(mPauseOrStart);
            } else if (iMediaPlaye.getCurrentState() == iMediaPlaye.STATE_PLAYBACK_COMPLETE) {
                iMediaPlaye.start();
                mPauseOrStart = true;
                playView.setSelected(mPauseOrStart);
                mDelayedTask.startForwardTimer(4000, 0);
                mDelayedTask2.startForwardTimer(1000, 1);
            }
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.view_player_control_back) {
            clickBack();
        } else if (v.getId() == R.id.view_player_control_mPlay) {
            doubleTap(null);
            mDelayedTask.startForwardTimer(4000, 0);
        } else if (v.getId() == R.id.view_player_control_mFullscreen) {
            clickFullscreen();
            mDelayedTask.startForwardTimer(4000, 0);
        }
    }

    private void clickFullscreen() {
        if (getContext() instanceof Activity) {
            Activity activity = (Activity) getContext();
            if (activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                fullscreenView.setSelected(true);
            } else if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                fullscreenView.setSelected(false);
            }
        }
    }

    private void clickBack() {
        if (getContext() instanceof Activity) {
            Activity activity = (Activity) getContext();
            if (activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
                activity.finish();
            } else if (((Activity) getContext()).getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
                clickFullscreen();
            }
        }
    }

    @Override
    protected boolean singleTapConfirmed(MotionEvent e) {
        if (isHideControlBar) {
            showControlBar();
        } else {
            hideControlBar();
        }
        return true;
    }

    @Override
    public void gestureControl(boolean isStart) {
        if (isStart) {
            if (!isHideControlBar)
                hideControlBar();
        }
    }
    public void showControlBar() {
        isHideControlBar = false;
        mDelayedTask.startForwardTimer(4000, 0);
        buildObjectAnimator(controlView, 0.0f, 1.0f).setDuration(500).start();
    }

    public void hideControlBar() {
        isHideControlBar = true;
        mDelayedTask.stopForwardTimer();
        buildObjectAnimator(controlView, 1.0f, 0.0f).setDuration(500).start();
    }

    private AnimatorSet buildObjectAnimator(View view, float starta, float ende) {
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, View.ALPHA, starta, ende);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator3);
        return set;
    }

    @Override
    public void onHandleMessage(int msg) {
        switch (msg) {
            case 0:
                hideControlBar();
                break;
            case 1:
                if (iMediaPlaye != null) {
                    setCurrentTime(iMediaPlaye.getCurrentPosition());
                }
                currentTimeView.setText(timeFormat(mCurrentTime));
                seekBar.setProgress((int) (mCurrentTime / 1000));
                mDelayedTask2.startForwardTimer(1000, 1);
                break;
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mDelayedTask2 != null) {
            mDelayedTask2.setOnDelayedTaskListener(null);
            mDelayedTask2.stopForwardTimer();
            mDelayedTask2 = null;
        }
        if (mDelayedTask != null) {
            mDelayedTask.setOnDelayedTaskListener(null);
            mDelayedTask.stopForwardTimer();
            mDelayedTask = null;
        }
        if (iMediaPlaye != null) {
            mDelayedTask = null;
        }
    }

    ///////////////左右滑动调整进度///////////////
    @Override
    protected void startSeek() {
        onStartTrackingTouch(seekBar);
    }

    @Override
    protected void seekChanged(long time) {
        onProgressChanged(seekBar, ((int) time / 1000), false);
    }

    @Override
    protected void stopSeek(long time) {
        onStopTrackingTouch(seekBar);
    }

    ///////////////seekBar拖拽调整进度///////////////
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        seekBar.setProgress(progress);
        setCurrentTime(progress * 1000);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        mDelayedTask2.stopForwardTimer();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (iMediaPlaye != null) {
            iMediaPlaye.seekTo(mCurrentTime);
            mDelayedTask2.startForwardTimer(1000, 1);
        }
    }
}
